@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM PPH System File Copy Script
REM Copy necessary files from source project to deployment package

echo ========================================
echo PPH Production Package File Copy
echo ========================================
echo.

REM Set source project path (modify according to your actual situation)
REM Default path, please modify according to your actual situation
set "SOURCE_DIR=d:\OneDrive\Desktop\新PPH项目"
set "DEPLOY_DIR=%~dp0.."
set "APP_DIR=%DEPLOY_DIR%\app"

echo [INFO] PPH Project File Copy Script
echo [INFO] This script copies files from source project to deployment package
echo.
echo [IMPORTANT] Usage Instructions:
echo 1. Local Test: Run this script on development machine to test file copy
echo 2. Server Deploy: Copy entire deployment package to server then run
echo 3. Path Config: Please confirm the source path below is correct
echo.

echo [INFO] Source directory: %SOURCE_DIR%
echo [INFO] Deploy directory: %DEPLOY_DIR%
echo [INFO] App directory: %APP_DIR%
echo.

REM Check if source directory exists
if not exist "%SOURCE_DIR%" (
    echo [ERROR] Source directory not found: %SOURCE_DIR%
    echo.
    echo [SOLUTION] Please follow these steps:
    echo 1. If local: Modify SOURCE_DIR path in this script
    echo 2. If server: Copy original project to server first, then modify path
    echo 3. Or manually copy files to app\ directory
    echo.
    echo [MANUAL COPY] Manual copy guide:
    echo - Copy backend\ folder to %APP_DIR%\backend\
    echo - Copy frontend\ folder to %APP_DIR%\frontend\
    echo - Copy database file to %APP_DIR%\backend\data\
    echo.
    pause
    exit /b 1
)

REM Create target directory structure
echo [STEP 1] Creating Directory Structure
echo ========================================

if not exist "%APP_DIR%" mkdir "%APP_DIR%"
if not exist "%APP_DIR%\backend" mkdir "%APP_DIR%\backend"
if not exist "%APP_DIR%\frontend" mkdir "%APP_DIR%\frontend"
if not exist "%APP_DIR%\data" mkdir "%APP_DIR%\data"
if not exist "%APP_DIR%\logs" mkdir "%APP_DIR%\logs"

echo [OK] Directory structure created
echo.

REM Copy backend files
echo [STEP 2] Copying Backend Files
echo ========================================

echo [INFO] Copying backend source files...

REM Copy backend core files
robocopy "%SOURCE_DIR%\backend" "%APP_DIR%\backend" /E /XD node_modules .git logs uploads temp /XF *.log *.tmp package-lock.json

REM Ensure key files exist
if exist "%SOURCE_DIR%\backend\server-simple.cjs" (
    copy "%SOURCE_DIR%\backend\server-simple.cjs" "%APP_DIR%\backend\"
    echo [OK] server-simple.cjs copied
) else (
    echo [WARNING] server-simple.cjs not found in source
)

if exist "%SOURCE_DIR%\backend\package.json" (
    copy "%SOURCE_DIR%\backend\package.json" "%APP_DIR%\backend\"
    echo [OK] backend package.json copied
) else (
    echo [WARNING] backend package.json not found
)

REM Copy database file
if exist "%SOURCE_DIR%\backend\data\pph.sqlite" (
    if not exist "%APP_DIR%\backend\data" mkdir "%APP_DIR%\backend\data"
    copy "%SOURCE_DIR%\backend\data\pph.sqlite" "%APP_DIR%\backend\data\"
    echo [OK] Database file copied
) else (
    echo [WARNING] Database file not found, will need to initialize
)

echo [OK] Backend files copied
echo.

:: 复制前端文件
echo [STEP 3] Copying Frontend Files
echo ========================================

echo [INFO] Copying frontend source files...

:: 复制前端源码
robocopy "%SOURCE_DIR%\frontend" "%APP_DIR%\frontend" /E /XD node_modules .git dist /XF *.log *.tmp package-lock.json

:: 确保关键文件存在
if exist "%SOURCE_DIR%\frontend\package.json" (
    copy "%SOURCE_DIR%\frontend\package.json" "%APP_DIR%\frontend\"
    echo [OK] frontend package.json copied
) else (
    echo [WARNING] frontend package.json not found
)

if exist "%SOURCE_DIR%\frontend\vite.config.ts" (
    copy "%SOURCE_DIR%\frontend\vite.config.ts" "%APP_DIR%\frontend\"
    echo [OK] vite.config.ts copied
) else (
    echo [WARNING] vite.config.ts not found
)

if exist "%SOURCE_DIR%\frontend\index.html" (
    copy "%SOURCE_DIR%\frontend\index.html" "%APP_DIR%\frontend\"
    echo [OK] index.html copied
) else (
    echo [WARNING] index.html not found
)

:: 复制静态服务器文件
if exist "%SOURCE_DIR%\frontend\static-server.cjs" (
    copy "%SOURCE_DIR%\frontend\static-server.cjs" "%APP_DIR%\frontend\"
    echo [OK] static-server.cjs copied
) else (
    echo [WARNING] static-server.cjs not found
)

echo [OK] Frontend files copied
echo.

:: 复制根目录文件
echo [STEP 4] Copying Root Files
echo ========================================

:: 复制根目录的package.json（如果存在）
if exist "%SOURCE_DIR%\package.json" (
    copy "%SOURCE_DIR%\package.json" "%DEPLOY_DIR%\app\"
    echo [OK] Root package.json copied
)

:: 复制数据库初始化文件
if exist "%SOURCE_DIR%\db" (
    robocopy "%SOURCE_DIR%\db" "%APP_DIR%\db" /E
    echo [OK] Database schema files copied
)

:: 复制必要的脚本文件
if exist "%SOURCE_DIR%\scripts" (
    if not exist "%APP_DIR%\scripts" mkdir "%APP_DIR%\scripts"
    
    :: 只复制必要的脚本
    if exist "%SOURCE_DIR%\scripts\port-manager.cjs" copy "%SOURCE_DIR%\scripts\port-manager.cjs" "%APP_DIR%\scripts\"
    if exist "%SOURCE_DIR%\scripts\process-guardian.cjs" copy "%SOURCE_DIR%\scripts\process-guardian.cjs" "%APP_DIR%\scripts\"
    
    echo [OK] Essential scripts copied
)

echo [OK] Root files copied
echo.

:: 复制缺失的关键文件
echo [STEP 5] Copying Missing Critical Files
echo ========================================

:: 检查并复制check-staff-fields.cjs（已知的缺失文件）
if exist "%SOURCE_DIR%\check-staff-fields.cjs" (
    copy "%SOURCE_DIR%\check-staff-fields.cjs" "%APP_DIR%\"
    echo [OK] check-staff-fields.cjs copied
) else (
    echo [WARNING] check-staff-fields.cjs not found in source root
)

:: 检查其他可能缺失的文件
if exist "%SOURCE_DIR%\legacyDb.db" (
    copy "%SOURCE_DIR%\legacyDb.db" "%APP_DIR%\"
    echo [OK] legacyDb.db copied
)

echo [OK] Critical files check completed
echo.

:: 创建必要的空目录
echo [STEP 6] Creating Required Empty Directories
echo ========================================

if not exist "%APP_DIR%\backend\uploads" mkdir "%APP_DIR%\backend\uploads"
if not exist "%APP_DIR%\backend\logs" mkdir "%APP_DIR%\backend\logs"
if not exist "%APP_DIR%\logs" mkdir "%APP_DIR%\logs"
if not exist "D:\PPH-Backups" mkdir "D:\PPH-Backups"

echo [OK] Required directories created
echo.

:: 设置文件权限
echo [STEP 7] Setting File Permissions
echo ========================================

echo [INFO] Setting appropriate file permissions...
icacls "%APP_DIR%" /grant Everyone:F /T >nul 2>&1
icacls "D:\PPH-Backups" /grant Everyone:F /T >nul 2>&1

echo [OK] File permissions set
echo.

:: 验证复制结果
echo [STEP 8] Verification
echo ========================================

echo [INFO] Verifying copied files...

:: 检查关键文件
set "MISSING_FILES="

if not exist "%APP_DIR%\backend\server-simple.cjs" (
    echo [ERROR] Missing: backend\server-simple.cjs
    set "MISSING_FILES=1"
)

if not exist "%APP_DIR%\backend\package.json" (
    echo [ERROR] Missing: backend\package.json
    set "MISSING_FILES=1"
)

if not exist "%APP_DIR%\frontend\package.json" (
    echo [ERROR] Missing: frontend\package.json
    set "MISSING_FILES=1"
)

if not exist "%APP_DIR%\frontend\index.html" (
    echo [ERROR] Missing: frontend\index.html
    set "MISSING_FILES=1"
)

if "%MISSING_FILES%"=="1" (
    echo [WARNING] Some critical files are missing
    echo Please check the source directory and copy manually if needed
) else (
    echo [OK] All critical files are present
)

:: 显示目录结构
echo.
echo [INFO] Final directory structure:
tree /f "%APP_DIR%" | more

echo.
echo ========================================
echo FILE COPY COMPLETED!
echo ========================================
echo.
echo Next steps:
echo 1. Review the copied files
echo 2. Run: scripts\install-dependencies.bat
echo 3. Run: scripts\deploy-cmd.bat
echo.
echo Source: %SOURCE_DIR%
echo Target: %APP_DIR%
echo.

pause
